const ProblemModel = require('../db/models/problem');


class ProblemService { 
  async addProblem (data) {
    //检查cid是否存在 check cid is existed
    const pid = data.pid;
    const result = await ProblemModel.findOne({ where: { pid } });

    //判断 check
    if (result) { 
      //更新 update  
      return await ProblemModel.update(data, {where: { pid }})
    }else {
      //创建 create
      return await ProblemModel.create(data);
    }
  }

  async getAllProblems () {
    return await ProblemModel.findAll();
  }

  //查 pid
  async findProblemById(pid) {
    const problem = await ProblemModel.findOne({
      where: { pid }
    });

    return problem;
  }

  async findProblemByIdGroup(pidArr) {
    return await ProblemModel.findAll({
      where: { pid: pidArr }
    });
  }

  async findProblemByTypeId (problemTypeId) {
    return await ProblemModel.findAll({
      where: { problemTypeId }
    })
  }

  async deleteProblemById (pid) {
    const result = await ProblemModel.findOne({ where: { pid } });
    return result.destroy();
  }
}

module.exports = new ProblemService();